<?php
session_start();
if (empty($_SESSION['username'])) {
    echo '<script type="text/javascript">window.location.href="../index.php";</script>';
} else {
    $now = time();
    if ($now > $_SESSION['expire']) {
        session_destroy();
        echo '<script type="text/javascript">var r=confirm("Session expire (30 mins)!"); if(r==true){window.location.href="../index.php";}else{window.location.href="index.php";}</script>';
    } else {
        require '../model-db-connection/config.php';
        $config = require '../model-db-connection/qrc_conf.properties.php';
        $search_condition = $_GET['search_condition'];
        $memId = $_GET['memId'];
        $memName = $_GET['memName'];
        $memRole = $_GET['memRole'];
        $memberSkill = $_GET['memberSkill'];
        $teamCode = $_GET['teamCode'];
        $teamName = $_GET['teamName'];
        $searchLimit = $_GET['searchLimit'];
    }
}
?>
<script src="../js/jquery.dataTables.js"></script>
<script src="../js/plugins/datatables/ColReorderWithResize.js"></script>
<script src="../js/plugins/datatables/colvis/dataTables.ColVis.min.js"></script>
<script src="../js/plugins/datatables/tabletools/ZeroClipboard.js"></script>
<script src="../js/plugins/datatables/tabletools/dataTables.TableTools.min.js"></script>
<script src="../js/dataTables.bootstrap.js"></script>

<link rel="stylesheet" href="../css/plugins/dataTables.bootstrap.css">
<link rel="stylesheet" href="../css/ColVis.css">

<script type="text/javascript" src="../js/zebra_dialog.js"></script>
<link rel="stylesheet" href="../css/zebra_dialog.css" type="text/css">

<table class="table table-striped table-bordered table-hover" id="dataTables-example" width="100%">
    <thead>
        <tr>
            <th data-class="expand" class="center">Project Code</th>
            <th class="center">Project Name</th>
            <th data-hide="phone,tablet">Project Manager</th>
<!--            <th data-hide="phone,tablet">Project Foreman</th>
            <th data-hide="phone,tablet">Supervisor Control</th>-->
            <th data-hide="phone,tablet">Team Owner</th>
<!--            <th data-hide="phone,tablet">Quality Inspectors</th>
            <th data-hide="phone,tablet">Address/Location</th>
            <th data-hide="phone,tablet">Remark</th>-->
            <th class="center">Project Type</th>
            <th class="center">Project Status</th>
            <th class="center">Project Owner</th>                                                        
            <th data-hide="phone,tablet">Customer Name</th>
            <th class="center"></th>
        </tr>
    </thead>
    <tbody>
        <?php
        if ($search_condition == "search_all") {

            $sqlSelectAllProjectRecord = "SELECT qp.project_code as project_code,"
                    . "qp.project_name as project_name,"
                    . "ps.project_status_name as project_status,"
                    . "po.project_owner_name as project_owner,"
                    . "pt.project_type_name as project_type,"
                    . "qcn.customer_name as customer_name,"
                    . "qp.project_manager as project_manager,"
                    . "qp.project_foreman as project_foreman,"
                    . "qp.supervisor_control as supervisor_control,"
                    . "qtb.tName as team_owner,"
                    . "qp.quality_inspectors as quality_inspectors,"
                    . "qp.project_remark as remark,"
                    . "qp.address_location as address_location"
                    . " FROM QRC_PROJECT qp"
                    . " LEFT JOIN PROJECT_STATUS ps ON qp.project_status = ps.project_status_id"
                    . " LEFT JOIN PROJECT_OWNER po ON qp.project_owner = po.project_owner_id"
                    . " LEFT JOIN PROJECT_TYPE pt on qp.project_type = pt.project_type_id"
                    . " LEFT JOIN QRC_CUSTOMER_NAME qcn on qp.customer_id = qcn.customer_id"
                    . " LEFT JOIN QRC_TEAM_BUILDER qtb ON qp.team_owner = qtb.tCode"
                    . " ORDER BY qp.created_date_time DESC"
                    . " LIMIT 100;";
        } else {
            if ($searchLimit == "All") {
                $limit = "";
            } else {
                $limit = " LIMIT " . $searchLimit . ";";
            }
            $checkProjectCode = !empty($projectCodeSearch) ? " AND qp.project_code LIKE '%$projectCodeSearch%'" : "";
            $checkProjectName = !empty($projectNameSearch) ? " AND qp.project_name LIKE '%$projectNameSearch%'" : "";
            $checkProjectType = !empty($projectTypeSearch) ? " AND pt.project_type_id LIKE '$projectTypeSearch'" : "";
            $checkProjectStatus = !empty($projectStatusSearch) ? " AND qp.project_status LIKE '$projectStatusSearch'" : "";
            $checkProjectOwner = !empty($projectOwnerSearch) ? " AND qp.project_owner LIKE '$projectOwnerSearch'" : "";
            $checkProjectCustomer = !empty($projectCustomerSearch) ? " AND qp.customer_id LIKE '$projectCustomerSearch'" : "";
            //$checkStartDate = !empty($startSearchDate) ? " AND qp.created_date_time BETWEEN '$startSearchDate' AND '$endSearchDate'" : "";
            $checkStartDate = !empty($startSearchDate) ? " AND qp.created_date_time >= '$startSearchDate' AND qp.created_date_time < '$endSearchDate'" : "";
            $sqlSelectAllProjectRecord = "SELECT qp.project_code as project_code,"
                    . "qp.project_name as project_name,"
                    . "ps.project_status_name as project_status,"
                    . "po.project_owner_name as project_owner,"
                    . "pt.project_type_name as project_type,"
                    . "pt.project_type_id as project_type_id,"
                    . "qcn.customer_name as customer_name,"
                    . "qp.project_manager as project_manager,"
                    . "qp.project_foreman as project_foreman,"
                    . "qp.supervisor_control as supervisor_control,"
                    . "qtb.tName as team_owner,"
                    . "qp.quality_inspectors as quality_inspectors,"
                    . "qp.project_remark as remark,"
                    . "qp.address_location as address_location"
                    . " FROM QRC_PROJECT qp"
                    . " LEFT JOIN PROJECT_STATUS ps ON qp.project_status = ps.project_status_id"
                    . " LEFT JOIN PROJECT_OWNER po ON qp.project_owner = po.project_owner_id"
                    . " LEFT JOIN PROJECT_TYPE pt on qp.project_type = pt.project_type_id"
                    . " LEFT JOIN QRC_CUSTOMER_NAME qcn on qp.customer_id = qcn.customer_id"
                    . " LEFT JOIN QRC_TEAM_BUILDER qtb ON qp.team_owner = qtb.tCode"
                    . " WHERE 1=1"
                    . $checkProjectCode
                    . $checkProjectName
                    . $checkProjectType
                    . $checkProjectStatus
                    . $checkProjectOwner
                    . $checkProjectCustomer
                    . $checkStartDate
                    . " ORDER BY qp.created_date_time DESC"
                    . $limit;
        }
        $sqlGetAllData = mysql_query($sqlSelectAllProjectRecord);
        if (mysql_num_rows($sqlGetAllData) >= 1) {
            while ($row = mysql_fetch_assoc($sqlGetAllData)) {

                echo '<tr class = "gradeX">';
                echo '<td>' . $row['project_code'] . '</td>';
                echo '<td class = "center">' . $row['project_name'] . '</td>';
                echo '<td>' . $row['project_manager'] . '</td>';
                //echo '<td>' . $row['project_foreman'] . '</td>';
                //echo '<td>' . $row['supervisor_control'] . '</td>';
                echo '<td>' . $row['team_owner'] . '</td>';
                $data = $row['quality_inspectors'];
                $sqlSelectSeletedMembers = "SELECT memName FROM QRC_MEMBERS WHERE memID like '$data';";
                $sqlGetQI = mysql_query($sqlSelectSeletedMembers);
                $rows = mysql_fetch_assoc($sqlGetQI);
                //echo '<td>' . $rows['memName'] . '</td>';
                //echo '<td>' . $row['address_location'] . '</td>';
                //echo '<td>' . $row['remark'] . '</td>';
                echo '<td class = "center">' . $row['project_type'] . '</td>';
                echo '<td class = "center">' . $row['project_status'] . '</td>';
                echo '<td class = "center">' . $row['project_owner'] . '</td>';
                echo '<td>' . $row['customer_name'] . '</td>';


                echo '<td>';

                echo '<div class = "btn-group margin-bottom-20">';
                echo '<button type = "button" class = "btn btn-default dropdown-toggle btn-xs" data-toggle = "dropdown">Actions <span class = "caret"></span></button>';

                echo '<ul class = "dropdown-menu" role = "menu">';
                echo '<li><a href = "#" class="btn-xs"  onclick=loadProjectOrder()><i class = "fa fa-rss"></i> Work Order</a></li>';
                echo '<li><a href = "#modal-login" class="btn-xs" data-toggle = "modal" onclick=editProject("' . $row['project_code'] . '")><i class = "fa fa-edit"></i> Edit (แก้ไข)</a></li>';
                echo '<li><a href = "#" class="btn-xs" onclick=viewClick("' . $row['project_code'] . '") ><i class = "fa fa-eye"></i> View (ดูข้อมูล)</a></li>';
                echo '<li class = "divider"></li>';
                echo '<li><a href = "#" class="btn-xs" onclick=deleteProject("' . $row['project_code'] . '")><i class = "fa fa-trash-o"></i> Delete (ลบ)</a></li>';
                echo '</ul>';
                echo '</div>';
                echo '</td>';
                echo '</tr>';
            }
        }
        ?>                                          
    </tbody>
</table>
<script>
    $(document).ready(function() {
        var datableConfig = $('#dataTables-example').dataTable({
            "order": [[0, "desc"]]
        });
    });


    function viewClick(po_id) {
        $.Zebra_Dialog({
            source: {'ajax': 'AjaxViewContent.php?po_id=' + po_id},
            width: 600,
            title: 'Project Detail'
        });
    }
</script>